﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Recognizer;
using Util.Props;
using System.Diagnostics;
using Api;
using Common;
using System.IO;

namespace RecognizerUnitTest
{
    [TestClass]
    public class RecognizerUnitTest
    {
        [TestMethod]
        public void TestAPI()
        {
            Trace.WriteLine("Loading models...");

            Configuration configuration = new Configuration();

            // Load model from the jar
            configuration.setAcousticModelPath("tidigits");
        
            // You can also load model from folder
            // configuration.setAcousticModelPath("file:en-us");
        
            configuration.setDictionaryPath("tidigits/dict/dictionary");
            // not used for tidigits
            //configuration.LanguageModelPath = "models/language/en-us.lm.dmp";

            StreamSpeechRecognizer recognizer = new StreamSpeechRecognizer(configuration);
            FileStream stream = new FileStream("demo/aligner/10001-90210-01803.wav",FileMode.Open);

            // Simple recognition with generic model
            recognizer.startRecognition("D:\\AudioTest\\10001-90210-01803.wav");
        
            SpeechResult result;
            while ((result = recognizer.getResult()) != null) {
        
                Trace.WriteLine("Hypothesis: %s\n",
                                  result.getHypothesis());
                              
                Trace.WriteLine("List of recognized words and their times:");
                foreach (IWordResult r in result.getWords()) 
                {
                    Trace.WriteLine(r);
                }

                Trace.WriteLine("Best 3 hypothesis:");            
                foreach (String s in result.getNbest(3))
                    Trace.WriteLine(s);

                Trace.WriteLine("Lattice contains " + result.getLattice().getNodes().Count + " nodes");
            }
            recognizer.stopRecognition();
    
        
            //// Live adaptation to speaker with speaker profiles
 
            //stream = TranscriberDemo.class.getResourceAsStream(
            //        "/edu/cmu/sphinx/demo/aligner/10001-90210-01803.wav");
        
            //// Stats class is used to collect speaker-specific data
            //Stats stats = recognizer.createStats(1);
            //recognizer.startRecognition(stream);
            //while ((result = recognizer.getResult()) != null) {
            //    stats.collect(result);
            //}
            //recognizer.stopRecognition();
        
            //// Transform represents the speech profile
            //Transform transform = stats.createTransform();
            //recognizer.setTransform(transform);
        
            //// Decode again with updated transform
            //stream = TranscriberDemo.class.getResourceAsStream(
            //        "/edu/cmu/sphinx/demo/aligner/10001-90210-01803.wav");
            //recognizer.startRecognition(stream);
            //while ((result = recognizer.getResult()) != null) {
            //    System.out.format("Hypothesis: %s\n",
            //            result.getHypothesis());
            //}
            //recognizer.stopRecognition();
        }

        [TestMethod]
        public void ConfigurationTest()
        {
            String digitsConfig= "digits.xml";
            ConfigurationManager cm = new ConfigurationManager(digitsConfig);
            Recognizer.Recognizer sphinxDigitsRecognizer
                = (Recognizer.Recognizer)cm.lookup("recognizer");
            Boolean done = false;
            IResult result=null;

            sphinxDigitsRecognizer.allocate();

            //// echo spoken digits, quit when 'nine' is spoken


            String resultStr = String.Empty;
            while (!done)
            {
                result = sphinxDigitsRecognizer.recognize();
                resultStr += "\nResult:" + result;

                done = result.ToString().Equals("nine");
            }

            sphinxDigitsRecognizer.deallocate();
        }

    }
}
